package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.impl.dao.AreaDao;
import com.jiantong.user.model.Area;
import com.jiantong.user.service.AreaWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by mz on 6/7/17.
 *
 */
@Slf4j
@RpcProvider
@Service
public class AreaWriteServiceImpl implements AreaWriteService{

    private final AreaDao areaDao;

    @Autowired
    public AreaWriteServiceImpl(AreaDao areaDao){
        this.areaDao = areaDao;
    }


    @Override
    public Response<Long> addArea(Area area) {

        try{
            areaDao.create(area);
            return Response.ok(area.getId());
        }catch (Exception e){
            log.error("failed to create area", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to create area");
        }
    }

    @Override
    public Response<Boolean> updateArea(Area area) {
        //todo 考虑事物的写法
        return null;
    }

    @Override
    public Response<Boolean> deleteArea(Long id) {
        //todo 考虑事物的写法
        return null;
    }

    @Override
    public Response<Integer> deleteAreas(List<Long> ids) {
        //todo 考虑事物的写法
        return null;
    }
    @Override
    public Response<Boolean> create(Area area){
        try{
            return Response.ok(areaDao.create(area));
        }catch (Exception e){
            log.error("failed to create area ", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to create area");
        }
    }

}
